package cn.jinjimi.app.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ViewUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import javax.annotation.Nullable;

/**
 * Created by lforxeverc on 2017/7/19.
 */

public class RewardCardDialog extends Dialog implements View.OnClickListener {
    private FrescoImageView ivRewardCard;
    private ImageView ivClose;
    private String mImageUrl;
    private String mLinkUrl;
    private boolean isFirstIn = true;

    public RewardCardDialog(Context context) {
        this(context, R.style.TransparentDialog);
    }

    public RewardCardDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_reward_card);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ivRewardCard = (FrescoImageView) findViewById(R.id.iv_rewardcard);
        ivClose = (ImageView) findViewById(R.id.iv_close);
        ivRewardCard.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
        ivRewardCard.setControllerListener(new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
                if (isFirstIn) {
                    isFirstIn = false;
                    if (imageInfo == null || ivRewardCard == null) return;
                    float rate = AndroidUtils.getWindowWidth(getContext()) / 720f;
                    int maxWidth = ViewUtils.dip2px(getContext(), 350);
                    int width = (int) (imageInfo.getWidth() * rate);
                    int height = (int) (imageInfo.getHeight() * rate);
                    if (width > maxWidth) {
                        width = maxWidth;
                        height = height * maxWidth / width;
                    }
                    final int finalWidth = width;
                    final int finalHeight = height;
                    ivRewardCard.post(new Runnable() {
                        @Override
                        public void run() {
                            ViewGroup.LayoutParams layoutParams = ivRewardCard.getLayoutParams();
                            layoutParams.width = finalWidth;
                            layoutParams.height = finalHeight;
                            ivRewardCard.setLayoutParams(layoutParams);
                        }
                    });
                }
            }
        });
    }

    @EventTrace(value = EventID.APP_SALE_BONUS_SHOW)
    public void show(String imageUrl, String linkUrl) {
        show();
        ivRewardCard.loadView(imageUrl);
        mImageUrl = imageUrl;
        mLinkUrl = linkUrl;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_rewardcard:
                if (!StringUtils.isEmpty(mLinkUrl)) {
                    open(mLinkUrl);
                }
                break;
            case R.id.iv_close:
                closeClick();
                break;
        }
    }

    @EventTrace(EventID.APP_SALE_BONUS_SHUT)
    private void closeClick() {
        dismiss();
    }

    @EventTrace(value = EventID.APP_SALE_BONUS_GO, paramsK = {"product_type"}, paramsV = {"app"})
    private void open(@EventTraceParam(EventDataField.ParamTo) String url) {
        Router.create(getContext(), url).open();
        dismiss();
    }
}
